var util = require('../../../libs/util.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    middle_height: "100vh",
    delivery_date: util.dateObjectformat(new Date(), 'yyyy-MM'),
    delivery_date_text: "本月数据",
    fingerPrint: false,
    auth: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({ "title": app.appName +"--车辆统计" });

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          middle_height: res.windowHeight - 90
        });
      }
    });

    that.loadData();
  },
  changeDeliveryDate: function (e) {
    this.setData({
      delivery_date: e.detail.value,
      delivery_date_text: e.detail.value
    });
    this.loadData();
  },
  loadData: function () {
    var that = this;
    util.getData("/wechat/bsanalysis/allProjectIncome", { "month": that.data.delivery_date }, function (res) {
      console.log(res.data)
      if (res.data.success) {
        var data = res.data.data;
        var _vehicleData = {};
        for (var i = 0; i < data.length; i++) {
          if (_vehicleData[data[i].vehicleId]) {
            var pt_item = _vehicleData[data[i].vehicleId];
            pt_item.income = Number(pt_item.income) + Number(data[i].income);
            pt_item.pay = Number(pt_item.pay) + Number(data[i].pay);
          } else {
            _vehicleData[data[i].vehicleId] = { vehicle: data[i].vehicle, income: data[i].income, pay: data[i].pay }
          }
        }
        var vehicleData = [];
        for (var i in _vehicleData) {
          vehicleData.push({ name: _vehicleData[i].vehicle, income: Number(_vehicleData[i].income).toFixed(0), pay: Number(_vehicleData[i].pay).toFixed(0) })
        }
        that.setData({
          totalVehicle: vehicleData.length,
          deliveryData: data,
          vehicleData: vehicleData
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (!that.data.fingerPrint) {
      wx.checkIsSupportSoterAuthentication({
        success(res) {
          if (res.supportMode[0] == 'fingerPrint') {
            wx.startSoterAuthentication({
              requestAuthModes: ['fingerPrint'],
              challenge: '123456',
              authContent: '请用指纹解锁',
              success(res) {
                that.setData({
                  fingerPrint: true,
                  auth: false
                })
              }
            })
          }
          // res.supportMode = [] 不具备任何被SOTER支持的生物识别方式
          // res.supportMode = ['fingerPrint'] 只支持指纹识别
          // res.supportMode = ['fingerPrint', 'facial'] 支持指纹识别和人脸识别
        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      fingerPrint: false,
      auth: true
    })
  },
  gotoBusiness: function () {
    wx.redirectTo({
      url: '/pages/business/business'
    })
  },
  gotoVehicleStat: function () {
    wx.redirectTo({
      url: '/pages/business/vehicle_stat'
    })
  },
  gotoDriverStat: function () {
    wx.redirectTo({
      url: '/pages/business/driver_stat'
    })
  },
  gotoAnalysis: function () {
    wx.redirectTo({
      url: '/pages/business/analysis'
    })
  }
})